package com.demo.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.demo.service.RackReadService;
import com.demo.util.JSONReadUtil;

import net.sf.json.JSONObject;

/**
 * @author 李文彬
 * @date 2017年7月13日 下午8:13:30
 * @class
 * @用处 ：
 */
public class RackReadServiceImpl implements RackReadService {

	@Override
	public String readFile(String path) {
		return JSONReadUtil.readFile(path);
	}

	@Override
	public List<String> readJsonStr(Object json) {
		return JSONReadUtil.readJiGui(json);
	}

	@Override
	public List<String> readJson(String path) {
		return readJsonStr(readFile(path));
	}

	@Override
	public Map<String, List<String>> readDongHuan(String path, List<String> list) {
		List<String> dongHuan;
		Map<String, List<String>> mapDongHuan = new HashMap<String, List<String>>();
		if (null != list) {
			for (int i = 0; i < list.size(); i++) {
				dongHuan = new ArrayList<String>();
				dongHuan = JSONReadUtil.readDongHuan(path, list.get(i));
				mapDongHuan.put(list.get(i), dongHuan);
			}
		}
		return mapDongHuan;
	}

	@Override
	public Map<String, List<String>> readDongHuan(Object object, List<String> list) {
		List<String> dongHuan;
		Map<String, List<String>> mapDongHuan = new HashMap<String, List<String>>();
		if (null != list) {
			for (int i = 0; i < list.size(); i++) {
				dongHuan = new ArrayList<String>();
				dongHuan = JSONReadUtil.readDongHuan(object, list.get(i));
				mapDongHuan.put(list.get(i), dongHuan);
			}
		}
		return mapDongHuan;
	}

	@Override
	public List<String> readJson(String path, List<String> fangJian) {
		Object json = JSONReadUtil.readFile(path);
		List<String> listRack = new ArrayList<String>();
		if (json == null) {
			return null;
		}
		try {
			JSONObject jsonObj = JSONObject.fromObject(json);
			Iterator it = jsonObj.keys();
			while (it.hasNext()) {
				String key = (String) it.next();
				Object value = jsonObj.get(key);
				JSONObject jo = JSONObject.fromObject(value);
				if ("机柜".equals(jo.get("CLSID"))) {
					for (int i = 0; i < fangJian.size(); i++) {
						if (fangJian.get(i).equals(jo.get("_PARENT_"))) {
							listRack.add(key);
						}
					}

				}
			}
		} catch (Exception e) {
		}
		return listRack;
	}

	@Override
	public Map<String, List<String>> readDongHuan(String path, List<String> list, List<String> fangJian) {
		List<String> dongHuan;
		Map<String, List<String>> mapDongHuan = new HashMap<String, List<String>>();
		if (null != list) {
			for (int i = 0; i < list.size(); i++) {
				dongHuan = new ArrayList<String>();
				dongHuan = JSONReadUtil.readDongHuan(path, list.get(i), fangJian);
				mapDongHuan.put(list.get(i), dongHuan);
			}
		}
		return mapDongHuan;

	}

	/*
	 * public static void main(String[] args) { RackReadService rackReadService=new
	 * RackReadServiceImpl(); String
	 * path="C:\\Users\\Administrator\\Desktop\\20170730121836636493519.json"; List
	 * list=new ArrayList<String>(); List list1=new ArrayList<String>(); Object
	 * object=rackReadService.readFile(path); //list.add("空调"); list.add("配电柜");
	 * list.add("UPS"); list.add("温湿度"); list1.add("4F电力电池室A");
	 * //System.out.println( JSONArray.fromObject(rackReadService.readJson(path)));
	 * System.out.println(JSONObject.fromObject((Object)rackReadService.
	 * readDongHuan(path, list,list1))); }
	 */
}
